/*
 * @Author: wkh
 * @Date: 2021-11-12 16:22:45
 * @LastEditTime: 2021-11-12 16:22:45
 * @LastEditors: wkh
 * @Description: 
 * @FilePath: /kcp-cpp/include/Trace.hpp
 * 
 */

#ifndef TRACE_HPP_
#define TRACE_HPP_

#include <iostream>
#include <time.h>
#include "util.hpp"
#include <string.h>
#include <stdio.h>


//#define DEBUG_
//#define WITH_COLOR_

// debug mode : trace information and state of the code for debuging.
#ifdef  DEBUG_

  #ifndef WITH_COLOR_
    #define TRACE(...) Trace( "[", __FILE__,":",__func__,":",__LINE__,"]", __VA_ARGS__)
  #else  // WITH_COLOR_
    #define TRACE(...) Trace( RED, "[", __FILE__,":",__func__,":",__LINE__,"]",  YELLOW, __VA_ARGS__, WHITE)
  #endif // WITH_COLOR_

  #define TRACE2(...) 

#else  //not define DEBUG_

  #define TRACE(...)                                          

  #ifndef WITH_COLOR_
    #define TRACE2(...) Trace( "[", __FILE__,":",__func__,":",__LINE__,"]", __VA_ARGS__)
  #else  // WITH_COLOR_
    #define TRACE2(...) Trace( RED, "[", __FILE__,":",__func__,":",__LINE__,"]",  YELLOW, __VA_ARGS__, WHITE)
  #endif // WITH_COLOR_

#endif // DEBUG_

// normal mode : trace for normol destination.
#define INFO(...) Trace( __VA_ARGS__)


//the following are UBUNTU/LINUX ONLY terminal color codes.
#define RESET   "\033[0m"
#define BLACK   "\033[30m"      /* Black */
#define RED     "\033[31m"      /* Red */
#define GREEN   "\033[32m"      /* Green */
#define YELLOW  "\033[33m"      /* Yellow */
#define BLUE    "\033[34m"      /* Blue */
#define MAGENTA "\033[35m"      /* Magenta */
#define CYAN    "\033[36m"      /* Cyan */
#define WHITE   "\033[37m"      /* White */
#define BOLDBLACK   "\033[1m\033[30m"      /* Bold Black */
#define BOLDRED     "\033[1m\033[31m"      /* Bold Red */
#define BOLDGREEN   "\033[1m\033[32m"      /* Bold Green */
#define BOLDYELLOW  "\033[1m\033[33m"      /* Bold Yellow */
#define BOLDBLUE    "\033[1m\033[34m"      /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m"      /* Bold Magenta */
#define BOLDCYAN    "\033[1m\033[36m"      /* Bold Cyan */
#define BOLDWHITE   "\033[1m\033[37m"      /* Bold White */



char* trim_string(char *str)
{
	int len = strlen(str);

	//去掉最后的换行符
	if(str[len-1] == '\n')
	{
		len--;		//字符串长度减一
		str[len] = 0;	//给字符串最后一个字符赋0，即结束符
	}
  return str;
}

//for debug
template<typename T ,typename... Args>
inline void trace(T&& tmp , Args&&... args)
{
    if constexpr(sizeof...(args)  > 0)
    {
        std::cout << tmp << " ";
        trace(std::forward<Args>(args)...);
    }else
    {
        
        std::cout << tmp <<std::endl;
       
    }
}

template<typename... Args>
inline void Trace(Args&&... args)
{
  time_t curtime;
  time(&curtime);
#ifdef WITH_COLOR_
  std::cout << BLUE ;
#endif
  std::cout <<"[";

  std::cout << trim_string(ctime(&curtime)); 

  std::cout <<"-";

  std::cout << iclock64();

  std::cout <<"]";


#ifdef WITH_COLOR_
  std::cout << WHITE ;
#endif
  trace(std::forward<Args>(args)...);
}



#endif
